<?php
namespace Home\Controller;
use Think\Controller;
//header("content-type:text/html;charset=utf-8");编码格式

class AuthController extends Controller{
	//用户授权获取code
	public function index(){
	    //授权完跳转的网址
	    $path = $_REQUEST['pgv_pvi'];
	    // dump($_REQUEST);
	    // dump($_SERVER);
	    echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'/home/auth/callback';
	    // die;
	    //用户同意授权后回调的网址.必须使用url对回调网址进行编码，我们也将授权完跳转对网址,
	    $redirect_uri = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'/home/auth/callback');
	    header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='
	    .C('appID').'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state='.$path.
	        '#wechat_redirect');
	}
	//获取code后的回调函数
	public function callback(){
	    //获取到的code
	    $code = $_REQUEST['code'];
	 
	    //授权结束后的回调网址
	    $path = $_REQUEST['state'];
	 
	    //获取access_token
	    $curl = curl_init();
	 
	    curl_setopt($curl,CURLOPT_URL,'https://api.weixin.qq.com/sns/oauth2/access_token?appid='
	        .C('appID').'&secret='.C('appSecret').'&code='.$code.'&grant_type=authorization_code ');
	 
	    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
	 
	    //获取access_token和openid,转换为数组
	    $data = json_decode(curl_exec($curl),true);
	 
	    //如果获取成功，根据access_token和openid获取用户的基本信息
	    if($data != null && $data['access_token']){
	 
	        //获取用户的基本信息，并将用户的唯一标识保存在session中
	        curl_setopt($curl,CURLOPT_URL,'https://api.weixin.qq.com/sns/userinfo?access_token='
	            .$data['access_token'].'&openid='.$data['openid'].'&lang=zh_CN');
	 
	        $user_data = json_decode(curl_exec($curl),true);
	 
	        if($user_data != null && $user_data['openid']){
	 
	            curl_close($curl);
	            //将用户信息存在数据库中,同时将用户在数据库中唯一的标识保存在session中
	            $array = [];
	 
	            $array['openid'] = $user_data['openid'];
	            $array['nickname'] = $user_data['nickname'];
	            $array['headimgurl'] = $user_data['headimgurl'];
	 
	            //我这里只存储了用户的openid,nickname,headimgurl
	            $model = M('user');
	            //先判断用户数据是不是已经存储了，如果存储了获取用户在数据库中的唯一标识
	            $user_id = $model->where(['openid'=>$array['openid']])->getField('user_id');
	            if($user_id){
	                session('user_id',$user_id);
	            }else{
	                $user_id = $model->add($array);
	                //将用户在数据库中的唯一表示保存在session中
	                session('user_id',$user_id);
	            }
	            //跳转网页
	            header('Location:'.$path);
	        }else{
	 
	            curl_close($curl);
	 
	            exit('获取用户信息失败！');
	 
	        }
	    }else{
	 
	        curl_close($curl);
	 
	        exit('微信授权失败');
	    }
	}
}